home *** CD-ROM | disk | FTP | other *** search
- Path: s02.pavilion.co.uk!usenet
- From: AJRobb@pavilion.co.uk (Andy J Robb)
- Newsgroups: comp.lang.c
- Subject: Re: RETURN ();
- Date: Thu, 08 Feb 1996 06:59:40 GMT
- Organization: Pavilion Internet plc
- Message-ID: <4fc6ve$kbn@s02.pavilion.co.uk>
- References: <DMFxxq.7M7@emi.net>
- NNTP-Posting-Host: poolb09.pavilion.co.uk
- X-Newsreader: Forte Free Agent 1.0.82
-
- samstar@emi.net wrote:
-
- >Is there a way to return muliple values to main from a seperate function ?
-
- >ex :
-
- >int main(void)
-
- main(int, char**) /* This says "ignore the command line" */
-
- >{
- > Input_data();
- > printf("Here all the input data");
- >
- > return 0;
- >}
-
- >int Input_data()
- >{
- > x=5,y=30,z=10 /* These were gotten by asking questions */
- It is not a good idea to use commas like this.
- > /* using printf / scanf/fgets (what ever) */
- > /*point is they are gotten by question */
-
- >return(x,y,z);
-
- This evaluates x then y and finally z returning the value (int)z
-
- >}
-
- >how will main be able to see these values ?
-
- main will only see the last value.
-
- There are several ways to do this. Arguably the clearest and most
- robust is to pass the addresses of the required results. There are
- other ways (including global variables) but these make the program
- more difficult to maintain.
-
- #include <stdio.h>
-
- void Input_data(int *x, int *y, int *z)
- {
- *x = 5;
- *y = 30;
- *z = 10;
- }
-
- main(int, char**)
- { int x, y, z;
- Input_data(&x, &y, &z);
-
- printf("here are the data: x=\"%d\", y=\"%d\", z=\"%d\"\n", x,y,z);
-
- return 0;
- }
-
- Regards,
- Andy Robb.
- -----BEGIN PGP PUBLIC KEY BLOCK-----
- Version: 2.6.2i
-
- mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
- MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
- B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
- tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
- IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
- =/wVD
- -----END PGP PUBLIC KEY BLOCK-----
-
-